package com.dispatcher;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.dispatcher.common.CommonDefine;
import com.dispatcher.view.PublicTopView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zuzhili.database.ZuZhiLiDBCtrl;

public class ActivityBase extends FragmentActivity{
	boolean mbIsQuit;
	byte[] mlock = new byte[0];
	private BroadcastReceiver br;
	protected boolean reciver = false;
	QuitListener mQuitListener;
	protected ImageLoader imageLoader = ImageLoader.getInstance();
	protected PublicTopView mtitleView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);

		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction("zuzhili.finish.action.broadcast");
		br = new MyBroadcastReciver();
		this.registerReceiver(br, intentFilter);

	}
	public ZuZhiLiDBCtrl getZuZhiLiDBCtrl() {
		return ((GlobalVar) getApplication()).g_dbctrl;
	}
//	protected void initTitleView() {
//		mtitleView = (PublicTopView) findViewById(R.id.head);
//	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			if (mQuitListener != null) {
				mQuitListener.execute(this);
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onStop() {
		imageLoader.stop();
		super.onStop();
	}

	@Override
	public void startActivityForResult(Intent it, int reqcode) {
		super.startActivityForResult(it, reqcode);
		overridePendingTransition(R.anim.activity_start_horizontal_in,
				R.anim.activity_start_horizontal_out);
	}

	@Override
	public void startActivity(Intent it) {
		super.startActivity(it);
		overridePendingTransition(R.anim.activity_start_horizontal_in,
				R.anim.activity_start_horizontal_out);
	}

	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.activity_finish_horizontal_in,
				R.anim.activity_finish_horizontal_out);
	}

	public DisplayImageOptions getHeadImageOption() {
		return ((GlobalVar) getApplication()).options_head;
	}

	public DisplayImageOptions getPicImageOption() {
		return ((GlobalVar) getApplication()).options_pic;
	}

	/*
	 * 取得字符�
	 */
	public String getstring(int id) {
		return CommonDefine.getResources(this).getString(id);
	}

	public void show_toast(int id) {
		Toast.makeText(this, getstring(id), 2000).show();// 显示两秒
	}

	public void show_toast(String str) {
		Toast.makeText(this, str, 2000).show();
	}


	@Override
	protected void onDestroy() {

		super.onDestroy();
		this.unregisterReceiver(br);

	}

	protected class LockInputFilter implements InputFilter {
		@Override
		public CharSequence filter(CharSequence source, int start, int end,
				Spanned dest, int dstart, int dend) {

			return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
		}
	}

	protected class UnLockInputFilter implements InputFilter {
		@Override
		public CharSequence filter(CharSequence source, int start, int end,
				Spanned dest, int dstart, int dend) {
			return null;
		}
	}

	protected void lockInput(EditText et) {
		et.setFilters(new LockInputFilter[] { new LockInputFilter() });
		et.setFocusable(false);
		et.setFocusableInTouchMode(false);
	}

	protected void unlockInput(EditText et) {
		et.setFilters(new UnLockInputFilter[] { new UnLockInputFilter() });
		et.setFocusable(true);
		et.setFocusableInTouchMode(true);
	}

	private class MyBroadcastReciver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals("zuzhili.finish.action.broadcast")) {

				if (reciver) {
					ActivityBase.this.finish();
				}
			}
		}
	}

//	void initTitle(int leftSrcid, int rightSrcid, String text,
//			String rbuttonText, OnClickListener left, OnClickListener right,
//			OnClickListener rightbutton) {
//		mtitleView = (PublicTopView) findViewById(R.id.head);
//		mtitleView.setLeftPicture(leftSrcid);
//		mtitleView.setTitleText(text);
//		mtitleView.setRightPicture(rightSrcid);
//		mtitleView.setLeftClickListener(left);
//		mtitleView.setRightClickListener(right);
//		mtitleView.setRightButtonText(rbuttonText);
//		mtitleView.setRightButtonClickListener(rightbutton);
//	}
	
//	void initTitle(int leftSrcid, int rightSrcid, String text,
//			String rbuttonText, OnClickListener left, OnClickListener right,
//			OnClickListener rightbutton, boolean isToleft) {
//		initTitle(leftSrcid, rightSrcid, text, rbuttonText, left, right, rightbutton);
//		if(isToleft == true) {
//			mtitleView.setTitleTextToLeft(text);
//		}
//	}

	public void checkEditText(final Context context, final EditText edt,
			final int maxWords) {
		/*edt.addTextChangedListener(new TextWatcher() {
			private CharSequence beforSequence;
			private int selectionStart;
			private int selectionEnd;

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// change before record
//				beforSequence = s;
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				beforSequence = s.;
			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
//				selectionStart = edt.getSelectionStart();
//				selectionEnd = edt.getSelectionEnd();
				int newlength=s.toString().length();
				if (newlength > maxWords) {
					Toast.makeText(context, "最多能输入" + maxWords + "个字符",
							Toast.LENGTH_SHORT).show();
					s.delete(beforSequence.length()-1, newlength);
					edt.setText(s);
				}
			}
		});*/
	}

	class QuitListener {
		boolean mb2main;

		public void setFlag2Main() {
			mb2main = true;
		}

		long mfirstbacktime;

		public void execute(Activity ctxOut) {
			long cur = System.currentTimeMillis();
			if (mfirstbacktime == 0 || (cur - mfirstbacktime) > 5000) {
				mfirstbacktime = System.currentTimeMillis();
				Toast.makeText(ctxOut, "再按一次退出软件", 2000).show();
				return;
			}
			if ((cur - mfirstbacktime) < 5000) {
				ctxOut.finish();
			}
			// if(mb2main){
			// AppMain.changed_trylucky();
			// return;
			// }
			// final Activity ctx=ctxOut;
			// Resources r = CommonDefine.getResources(ctx);
			// final CustomDlg dlg = new CustomDlg(ctx, R.style.mydialog);
			// dlg.setDisplayView(null, r.getString(R.string.toast_confirmquit),
			// r.getString(R.string.toast_confirm),
			// r.getString(R.string.toast_cancel));
			// dlg.setLBtnListner(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View arg0) {
			// dlg.cancel();
			// // Weibo.releaseInstance();
			// ctx.finish();
			// }
			// });
			// dlg.setRBtnListner(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View arg0) {
			// dlg.cancel();
			// }
			// });
			// dlg.show();
		}
	}

}